// pages/learn/course/details.js
import {
  $wuxDialog
} from '../../../miniprogram_npm/wux-weapp/index';
import {
  getUserCourse,
  getTouristCourse,
  getCourseOrderId
} from '../../../api/course';
import {
  pay
} from '../../../api/user';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    isIOS: false,
    isVip: false,
    index: 0,
    isMine: true,
    roleId:0,
    id: '',
    infoData: {},
    tagStyle: {
      p: 'line-height:24px;font-size:16px;color:#666666;letter-spacing:1px;margin-bottom:15rpx;',
      img: 'width:100vw;height:auto;',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      isMine: options.isMine === '1',
      id: Number(options.id),
      isLogin: getApp().globalData.isLogin,
      isIOS: getApp().globalData.isIOS,
      isVip: getApp().globalData.userInfo.vip,
      roleId:getApp().globalData.roleId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: getApp().globalData.isLogin,
      isVip: getApp().globalData.userInfo.vip,
    })
    this.loadData();
  },

  /**
   * 加载详情数据
   */
  async loadData() {
    let res;
    if (this.data.isLogin) {
      res = await getUserCourse(this.data.id);
    } else {
      res = await getTouristCourse(this.data.id);
    }
    this.setData({
      infoData: res
    })
  },
  jumpToCourse(e) {
    wx.navigateTo({
      url: '/pages/learn/course/details?id=' + e.detail,
    })
  },
  /**
   * 跳转视频播放
   * @param {object} e 
   */
  navigatorPlayVideo(e) {
    const {
      id,
      test,
    } = e.currentTarget.dataset;
    if ((this.data.isVip || this.data.infoData.buy  ||test  || this.data.infoData.price === 0 )  && this.data.roleId!==0 && this.data.roleId!==undefined) {
      wx.navigateTo({
        url: `../video/video?courseId=${this.data.id}&hourId=${id}`,
      })
    } else if(test){
      wx.navigateTo({
        url: `../video/video?courseId=${this.data.id}&hourId=${id}`,
      })
    }else {
    
      // if (this.data.isIOS) {
      //   $wuxDialog().alert({
      //     resetOnClose: true,
      //     title: '提示',
      //     content: 'iPhone用户请联系客服提供学习服务',
      //     onConfirm(e) {

      //     },
      //   })
      // } else {
      $wuxDialog().alert({
        resetOnClose: true,
        title: '提示',
        content: '本平台不开设个人会员服务，仅对机构，提交专业学堂申请',
        onConfirm(e) {},
      })
    }
    // }

  },

  jumpToRecord() {
    wx.navigateTo({
      url: '/pages/learn/record/record?id=' + this.data.id,
    })
  },

  /**
   * tabs 切换
   */
  handleTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = parseInt(key);
    this.setData({
      key,
      index,
    })
  },

  /**
   * swiper 切换
   */
  handleSwiperChange(e) {
    const {
      current
    } = e.detail
    const key = current.toString();
    this.setData({
      key,
      index: current,
    })
  },

  /**
   * 直接购买
   */
  async handlePayCourse() {
    await this.isLogin();

    let that = this;
    $wuxDialog().confirm({
      title: '提示',
      content: '确定购买该课程吗',
      buttons: [{
        text: '取消',
      }, {
        text: '去支付',
        type: 'primary',
        async onTap(e) {
          let orderId = await getCourseOrderId(that.data.id);
          pay(orderId).then(() => {
            wx.showToast({
              title: '购买成功',
              icon: 'success',
              mask: true,
            })
            that.setData({
              'infoData.buy': true,
            })
          })
        }
      }],
    })
  },

  /**
   * 跳转开通会员
   */
  navigatorMember() {
    this.isLogin().then(() => {
      if (this.data.isVip) {
        this.setData({
          index: 1,
          key: '1',
        })
      } else {
        wx.navigateTo({
          url: '/pages/user/member/index',
        })
      }

    })
  },

  /**
   * 判断是否登录
   */
  isLogin() {
    return new Promise(resolve => {
      if (this.data.isLogin) {
        resolve()
      } else {
        $wuxDialog().confirm({
          title: '你还没有登陆哦~',
          content: '请先登陆再查看更多内容',
          buttons: [{
            text: '暂不登录',
          }, {
            text: '立即登录',
            type: 'primary',
            onTap(e) {
              wx.navigateTo({
                url: '/pages/login/accredit/accredit',
              })
            }
          }],
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})